package com.lifu.yygh.hosp.controller.api;


import com.lifu.yygh.common.exception.YyghException;
import com.lifu.yygh.common.helper.HttpRequestHelper;
import com.lifu.yygh.common.result.Result;
import com.lifu.yygh.common.result.ResultCodeEnum;
import com.lifu.yygh.common.utils.MD5;
import com.lifu.yygh.hosp.service.DepartmentService;
import com.lifu.yygh.hosp.service.HospitalService;
import com.lifu.yygh.hosp.service.HosptialSetService;
import com.lifu.yygh.hosp.service.ScheduleService;
import com.lifu.yygh.model.hosp.Department;
import com.lifu.yygh.model.hosp.Hospital;
import com.lifu.yygh.model.hosp.Schedule;
import com.lifu.yygh.vo.hosp.DepartmentQueryVo;
import com.lifu.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Description
 * @Author lifu
 * @Date 2021/5/12 9:13
 */
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HosptialSetService hosptialSetService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;

    //上传医院接口
    @PostMapping(value = "/saveHospital")
    public Result saveHosp(HttpServletRequest request){
        //获取到传递的医院信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        //把String[]转成object
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(parameterMap);
       //获取医院系统传递的签名
        String sign = (String) stringObjectMap.get("sign");
        //去数据库查询医院编码的签名
        String hoscode = (String) stringObjectMap.get("hoscode");
        String signKey = hosptialSetService.getSignKey(hoscode);
        String signKeyMD5 = MD5.encrypt(signKey);
        if (!sign.equals(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //图片编码的问题
        String logoData = (String) stringObjectMap.get("logoData");
        logoData = logoData.replaceAll(" ","+");
        stringObjectMap.put("logoData",logoData);
        //调用service
        hospitalService.save(stringObjectMap);
        return Result.ok();
    }
    //查询医院接口
    @PostMapping(value = "/hospital/show")
    public Result getHospital(HttpServletRequest request){
        //获取到传递的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        //把String[]转成object
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        String hospSign = (String) paramMap.get("sign");
        String signKey = hosptialSetService.getSignKey(hoscode);
        String signKeyMD5 = MD5.encrypt(signKey);
        if (!hospSign.equals(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //调用service
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }
    //上传科室接口
    @PostMapping(value = "/saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        //获取到传递的科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        //把String[]转成object
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        String hospSign = (String) paramMap.get("sign");
        String signKey = hosptialSetService.getSignKey(hoscode);
        String signKeyMD5 = MD5.encrypt(signKey);
        if (!hospSign.equals(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //调用service
        departmentService.save(paramMap);
        return Result.ok();
    }
    //查询科室接口
    @PostMapping(value = "/department/list")
    public Result findDepartment(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //医院编号
        String hoscode = (String)paramMap.get("hoscode");
        //当前页
        Integer page = StringUtils.isEmpty(paramMap.get("page"))?1:Integer.parseInt((String) paramMap.get("page"));
        //每页记录数
        Integer limit = StringUtils.isEmpty(paramMap.get("limit"))?1:Integer.parseInt((String) paramMap.get("limit"));
        String hospSign = (String) paramMap.get("sign");
        String signKey = hosptialSetService.getSignKey(hoscode);
        String signKeyMD5 = MD5.encrypt(signKey);
        if (!hospSign.equals(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        Page<Department> pageModel = departmentService.findPageDepartment(page,limit,departmentQueryVo);
        return Result.ok(pageModel);
    }
    //删除科室接口
    @PostMapping(value = "/department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //医院编号和科室编号
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        //TODO 签名校验
        departmentService.remove(hoscode, depcode);
        return Result.ok();
    }
    //上传排版接口
    @PostMapping(value = "/saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        //获取到传递的排班信息
        Map<String, String[]> requestMap = request.getParameterMap();
        //把String[]转成object
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //TODO 签名校验
        //调用service
        scheduleService.save(paramMap);
        return Result.ok();
    }
    //查询排班接口
    @PostMapping(value = "/schedule/list")
    public Result findSchedule(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //医院编号科室编号
        String hoscode = (String)paramMap.get("hoscode");
        String depcode = (String)paramMap.get("depcode");
        //当前页
        Integer page = StringUtils.isEmpty(paramMap.get("page"))?1:Integer.parseInt((String) paramMap.get("page"));
        //每页记录数
        Integer limit = StringUtils.isEmpty(paramMap.get("limit"))?1:Integer.parseInt((String) paramMap.get("limit"));
        //TODO 签名校验
        ScheduleQueryVo scheduleQueryVo= new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setHoscode(depcode);
        Page<Schedule> pageModel = scheduleService.findPageSchedule(page,limit,scheduleQueryVo);
        return Result.ok(pageModel);
    }
    //删除科室接口
    @PostMapping(value = "/schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //医院编号和排班编号
        String hoscode = (String) paramMap.get("hoscode");
        String hosScheduleId = (String) paramMap.get("hosScheduleId");
        //TODO 签名校验
        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }
}
